package OOProgram;

public class Polymorphism {
    public static void main(String[] args) {
//    People p = new Man();
//    p.eat();
//    People p2 = new People();
//    p2.eat();
//        Apple apple = new Apple();
//        apple.eat();
//        apple.print();
//        Outer outer = new Outer("Outer");
//        Outer.Inner inner = outer.new Inner();//内部类必须得依赖外部类存在
//        Outer.StaticInner staticInner = new Outer.StaticInner();
//        staticInner.print();
        Outer outer = new Outer("Outer");
        Outer.StaticInner staticInner = new Outer.StaticInner();
        staticInner.print();
    }
}

abstract class People {
    public void eat() {
        System.out.println("吃东西");
    }

    public String Hello() {//如果父类的方法不允许被重写，可以加final修饰符
        return "hello world";
    }

    public abstract void run();//abstract方法，抽象方法不能有方法体，必须被重写
    //拥有抽象方法，则该类必须声明为抽象类，抽象类不能被实例化
}

class Man extends People {
    @Override
    public void eat() {
        System.out.println("吃苹果");
    }

    //@Overload
    public void eat(String food) {
        System.out.println("吃" + food);
    }

    @Override
    public String Hello() {
        return super.Hello() + "，i am Man";//在复写的方法中，如果要调用父类的方法，需要用super关键字
    }

    public void run() {
        System.out.println("跑");
    }
}

// 如果一个类没有字段，全是抽象方法，该类可以写成接口
//abstract class Fruit{
//    public abstract void eat();
//    public abstract void run();
//    public abstract void jump();
//}这种纯抽象类可以写成接口：
interface Fruit {
    public static final int count = 0;

    void eat();

    void run();

    void jump();

    default void print() {
        System.out.println("我是一个水果");
    }
}//类不能多继承，但可以实现多个接口继承

class Apple implements Fruit {
    @Override
    public void eat() {
        System.out.println("吃苹果");
    }

    @Override
    public void run() {
        System.out.println("吃苹果");
    }

    @Override
    public void jump() {
        System.out.println("吃苹果");
    }//该类必须实现所有的接口方法

    public void print() {
        System.out.println("我吃苹果");
    }//default方法可以实现,也可以不实现
}

class Outer {
    private String name;
    private static String staticName = "OUTER";

    public Outer(String name) {
        this.name = name;
    }

    class Inner {
        public void print() {
            System.out.println(name);
        }
    }

    static class StaticInner {
        public void print() {
            System.out.println("hello" + ' ' + staticName);
        }
    }
    class City {
        public String name;
        public Double latitude;
        public Double longitude; //field,字段，表示类的属性
    }

    // 多态
//阻止继承
    final class FinalClass {
    } //final 类不能被继承

    sealed class Shape permits Circle, Rectangle {
    }//只允许Circle, Rectangle继承Shape

    final class Circle extends Shape {
    }

    final class Rectangle extends Shape {
    }
}